#define  _CRT_SECURE_NO_WARNINGS 1

#include "BinaryTree.h"

BTNode* BuyBinaryTreeNode(BTDataType x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->data = x;
	newnode->left = newnode->right = NULL;

	return newnode;
}


BTNode* CreateBinaryTree(BTDataType* arr, int* pi)
{
	if (arr[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}

	BTNode* root = BuyBinaryTreeNode(arr[(*pi)++]);

	root->left = CreateBinaryTree(arr, pi);
	root->right = CreateBinaryTree(arr, pi);

	return root;
}


void PrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	else
	{
		printf("%c ", root->data);
		PrevOrder(root->left);
		PrevOrder(root->right);
	}
}


int TreeNodeSize(BTNode* root)
{
	return root == NULL ? 0 : 1 + TreeNodeSize(root->left) + TreeNodeSize(root->right);
}


BTNode* MergesBinaryTrees(BTNode* root1, BTNode* root2)
{
	if (root1 == NULL)
		return root2;
	else if (root2 == NULL)
		return root1;
	else
	{
		root1->data = root1->data + root2->data;
		root1->left = MergesBinaryTrees(root1->left, root2->left);
		root1->right = MergesBinaryTrees(root1->right, root2->right);

		return root1;
	}
}